package com.hu.zhcc.shiro.controller.xsx;


import com.hu.zhcc.shiro.entity.xsx.XsxTOrder;
import com.hu.zhcc.shiro.entity.xsx.XsxTOrdersetting;
import com.hu.zhcc.shiro.service.xsx.TOrderService;
import com.hu.zhcc.shiro.service.xsx.TOrdersettingService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 预约表 前端控制器
 * </p>
 *
 * @author xsx
 * @since 2021-06-11
 */
@RestController
@RequestMapping("/tOrder")
public class TOrderController {
    @Autowired
    TOrderService tOrderService;
    @Autowired
    TOrdersettingService tOrdersettingService;

    @PutMapping("/{jksid}")
    private List<XsxTOrder> queryAll(@PathVariable("jksid") Integer jksid){
        return tOrderService.queryOrderAndMember(jksid);
    }


    @PutMapping("/{zt}/id")
    private int qryy(@PathVariable("zt") Integer zt,Integer id){
        if(zt==0){
            return tOrderService.qryy(id);
        }
        if(zt==1){
            return tOrderService.qxyy(id);
        }
        return 0;
    }

    @PutMapping("/{yyid}/{tcid}/{yydate}/{xgdate}")
    private int yytc(@PathVariable("yyid") Integer yyid, Integer tcid, String yydate,String xgdate){
        XsxTOrder tOrder=new XsxTOrder();
        if(xgdate!=""){
            tOrder.setId(yyid);
            tOrder.setSetmealId(tcid);
            tOrder.setOrderDate(yydate);
            Integer integer = tOrdersettingService.queryCount(xgdate);
            XsxTOrdersetting tOrdersetting=new XsxTOrdersetting();
            if(integer==null){
                integer=0;
            }
            if(integer>0){
                integer=integer-1;
                tOrdersetting.setReservations(integer);
                tOrdersetting.setOrderDate(xgdate);
                tOrdersettingService.updateYyrs(tOrdersetting);
            }
            Integer integer2 = tOrdersettingService.queryCount(yydate);
            if(integer2==null){
                tOrdersetting.setReservations(1);
                tOrdersetting.setNumber(100);
                tOrdersetting.setOrderDate(yydate);
                tOrdersettingService.addSetting(tOrdersetting);
                integer2=0;
            }
            if(integer2>=0){
                integer2=integer2+1;
                tOrdersetting.setReservations(integer2);
                tOrdersetting.setOrderDate(yydate);
                tOrdersettingService.updateYyrs(tOrdersetting);
            }
            System.out.println(tOrder);
            return tOrderService.yytc(tOrder);
        }
        if(xgdate==""){
            tOrder.setMemberId(yyid);
            tOrder.setOrderType("电话预约");
            tOrder.setOrderStatus("未到诊");
            tOrder.setSetmealId(tcid);
            tOrder.setOrderDate(yydate);
            Integer rs = tOrdersettingService.queryCount(yydate);
            if(rs==null){
                rs=0;
            }
            XsxTOrdersetting tOrdersetting=new XsxTOrdersetting();
            if(rs>=0){
                rs=rs+1;
                tOrdersetting.setReservations(rs);
                tOrdersetting.setOrderDate(yydate);
                tOrdersettingService.updateYyrs(tOrdersetting);
            }
            if(rs==null){
                tOrdersetting.setReservations(1);
                tOrdersetting.setNumber(100);
                tOrdersetting.setOrderDate(yydate);
                tOrdersettingService.addSetting(tOrdersetting);
            }
            return tOrderService.yuyuetc(tOrder);
        }
        return 0;
    }

    @PostMapping("/{yydate}")
    private int isOkYy(@PathVariable("yydate") String yydate){
        Integer integer = tOrdersettingService.queryCount(yydate);
        if(integer==null){
            integer=0;
        }
        return integer;
    }

    @PostMapping("/{hyid}/num")
    private List<XsxTOrder> queryOrderByHyid(@PathVariable("hyid") Integer hyid, Integer num){
        return tOrderService.queryOrderByHyid(hyid);
    }
}

